#ifndef _NEW_VISITOR_H
#define _NEW_VISITOR_H

#include "translate.h"
#include <cstdlib>
#include <fstream>

using namespace std;
/**
* \class DisplayVisitor
* \author Layale Constantine & Jad Makhlouta 
* \brief defines the Visitor class structure for ProgGraph
*
*/
class DisplayVisitor2
{
private:
	
	ofstream tree; 
public:
	/**
	*\brief creates a tree.dot file and opens a digraph in it.
	*
	*/
	DisplayVisitor2() 
	{
		tree.open("tree2.dot");	
		tree<<"digraph parsing_tree {\n";
	}
	
	/*
	*\brief visits an ProgEdge by adding  it to the dot file.
	*
	*/
	bool operator ()(NewGraph::ProgEdge * edge)
	{
		tree<<"n"<<edge->from.idx<<"->n"<<edge->to.idx<<" [label=\""<< Label2string(edge->weight)<<"\"];\n";
        return true;
	}
	
	/**
	*\brief visits a ProgNode by adding it to the dot file.
	*
	*/
	bool operator()(NewGraph::ProgNode * node)
	{
		tree<<"n"<<node->idx<<" [label=\""<<node->data->toString();
    	if (node->type()==NewGraph::Variable)
    		tree<<","<<((NewGraph::ProgVar*)node)->name;
    	if (node->type()==NewGraph::Literal)
    		tree<<","<<((NewGraph::ProgLit*)node)->value;
    	tree<<"\"];\n";
        return true;
	}
	
	/**
	*\brief closes the dot file, and issues two system commands to create the svg graph and display it using a GNOME image viewer.
	*
	*/
	~DisplayVisitor2()
	{
		tree<<"}\n";
		tree.close();
		system("dot -Tsvg tree2.dot -o tree2.svg");
		system("eog tree2.svg &");
	}
};

typedef NewGraph::DFTraverse<DisplayVisitor2> NewGraphDFTraversal;

#endif
